package com.cipe.cmrs.controllers;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import com.cipe.cmrs.bean.CipeprojectBean;
import com.cipe.cmrs.exception.CipeprojectException;
import com.cipe.cmrs.model.Cipeproject;
import com.cipe.cmrs.model.Country;
import com.cipe.cmrs.model.Paymentrequestlineitem;
import com.cipe.cmrs.model.Region;
import com.cipe.cmrs.model.State;
import com.cipe.cmrs.model.User;
import com.cipe.cmrs.service.CipeprojectService;
import com.cipe.cmrs.service.CipeprojectstatusService;
import com.cipe.cmrs.service.CipeprojecttypeService;
import com.cipe.cmrs.service.CountryService;
import com.cipe.cmrs.service.DonorService;
import com.cipe.cmrs.service.DonorawardService;
import com.cipe.cmrs.service.PartnerService;
import com.cipe.cmrs.service.RegionService;
import com.cipe.cmrs.service.StateService;
import com.cipe.cmrs.service.UserService;
import com.cipe.cmrs.service.UsertypeService;
import com.cipe.cmrs.util.Constants;
import com.cipe.cmrs.util.Util;

@Controller
public class CipeprojectController extends BaseController {

	private static final long serialVersionUID = 1815579886981166259L;
	@Autowired
	private CipeprojectService cipeprojectService;
	@Autowired
	private CountryService countryService;
	@Autowired
	private CipeprojecttypeService cipeprojecttypeService;
	@Autowired
	private RegionService regionService;
	@Autowired
	private CipeprojectstatusService cipeprojectstatusService;
	@Autowired
	private DonorawardService donorawardService;
	@Autowired
	private UserService userService;
	@Autowired
	private UsertypeService usertypeService;
	// @Autowired
	// private CipeprojectrollupService cipeprojectrollupService;
	@Autowired
	private PartnerService partnerService;
	// @Autowired
	// private SubidentifierService subidentifierService;
	@Autowired
	private DonorService donorService;
	@Autowired
	private StateService stateService;
	private String editCipeprojectView = "editCipeproject";
	private static final String EDIT_PARTNER_GRANT_PAGE = "editPartnergrant";
	private static final String EDIT_PARTNER_GRANT_VIEW_PAGE = "editPartnergrantView";
	private static final String EDIT_CIPEPROJECT_DETAIL = "editCipeprojectDetail";

	@RequestMapping("/secure/findPartnergrant.do")
	public String findPartnergrant(Model model) {
		model.addAttribute("cipeproject", new Cipeproject());
		model.addAttribute("partnerList", partnerService.findAll());
		model.addAttribute("cipeprojecttypeList", cipeprojecttypeService.findAll());
		model.addAttribute("cipeprojectstatusList", cipeprojectstatusService.findAll());
		model.addAttribute("regionList", regionService.findAll());
		model.addAttribute("countryList", countryService.findAll());
		model.addAttribute("seniorProgramOfficerList", userService.findAll());
		model.addAttribute("programOfficerList", userService.findAll());
		model.addAttribute("programAssistantList", userService.findAll());
		return "findPartnergrant";
	}

	@RequestMapping("/secure/findCipeproject.do")
	public String findProject(Model model) {
		model.addAttribute("cipeproject", new Cipeproject());
		model.addAttribute("partnerList", partnerService.findAll());
		model.addAttribute("cipeprojecttypeList", cipeprojecttypeService.findAll());
		model.addAttribute("cipeprojectstatusList", cipeprojectstatusService.findAll());
		model.addAttribute("regionList", regionService.findAll());
		model.addAttribute("countryList", countryService.findAll());
		model.addAttribute("seniorProgramOfficerList", userService.findAllByOrderByFullName());
		model.addAttribute("programOfficerList", userService.findAllByOrderByFullName());
		model.addAttribute("programAssistantList", userService.findAllByOrderByFullName());
		return "findCipeproject";
	}

	@RequestMapping("/secure/findResultPartnergrant.do")
	public String findResultPartnergrant(@ModelAttribute("cipeproject") Cipeproject cipeproject, Model model) {
		model.addAttribute("cipeproject", cipeproject);
		model.addAttribute("partnerList", partnerService.findAll());
		model.addAttribute("cipeprojecttypeList", cipeprojecttypeService.findAll());
		model.addAttribute("cipeprojectstatusList", cipeprojectstatusService.findAll());
		model.addAttribute("regionList", regionService.findAll());
		model.addAttribute("countryList", countryService.findAll());
		model.addAttribute("seniorProgramOfficerList", userService.findAll());
		model.addAttribute("programOfficerList", userService.findAll());
		model.addAttribute("programAssistantList", userService.findAll());
		model.addAttribute("cipeprojectList", cipeprojectService.findPartnergrant(cipeproject));
		return "findPartnergrant";
	}

	@RequestMapping("secure/findProjectForm.do")
	public String findProjectForm() {
		return "findProjectForm";
	}

	@RequestMapping("/secure/findResultPartnergrantWithProcedure.do")
	public String findResultPartnergrantWithProcedure(@ModelAttribute("cipeproject") Cipeproject cipeproject, Model model) {
		model.addAttribute("cipeproject", cipeproject);
		model.addAttribute("partnerList", partnerService.findAll());
		model.addAttribute("cipeprojecttypeList", cipeprojecttypeService.findAll());
		model.addAttribute("cipeprojectstatusList", cipeprojectstatusService.findAll());
		model.addAttribute("regionList", regionService.findAll());
		model.addAttribute("countryList", countryService.findAll());
		model.addAttribute("seniorProgramOfficerList", userService.findAll());
		model.addAttribute("programOfficerList", userService.findAll());
		model.addAttribute("programAssistantList", userService.findAll());
		/* Request from paymentRequest view */
		if (cipeproject.getId() != null) {
			model.addAttribute("cipeprojectList", cipeprojectService.findCipeproject(cipeproject.getId(), cipeproject.getPrc()));
		} else {/* From Dashboard */
			model.addAttribute("cipeprojectList", cipeprojectService.findCipeproject(cipeproject.getPrc()));
		}
		model.addAttribute("message", cipeproject.getMessage());

		/*
		 * model.addAttribute("cipeprojectList",
		 * cipeprojectService.findPartnergrant(cipeproject));
		 */
		return "findPartnergrant";
	}

	@RequestMapping("/secure/findResultCipeproject.do")
	public String findResultProject(@ModelAttribute("cipeproject") Cipeproject cipeproject, Model model) {
		model.addAttribute("cipeproject", cipeproject);
		model.addAttribute("partnerList", partnerService.findAll());
		model.addAttribute("cipeprojecttypeList", cipeprojecttypeService.findAll());
		model.addAttribute("cipeprojectstatusList", cipeprojectstatusService.findAll());
		model.addAttribute("regionList", regionService.findAll());
		model.addAttribute("countryList", countryService.findAll());
		model.addAttribute("seniorProgramOfficerList", userService.findAll());
		model.addAttribute("programOfficerList", userService.findAll());
		model.addAttribute("programAssistantList", userService.findAll());
		model.addAttribute("cipeprojectList", cipeprojectService.findCipeproject(cipeproject));
		model.addAttribute("message", cipeproject.getMessage());
		return "findCipeproject";
	}

	@RequestMapping("/secure/findResultCipeprojectWithProcedure.do")
	public String findResultProjectWithProcedure(@ModelAttribute("cipeproject") Cipeproject cipeproject, Model model) {
		model.addAttribute("cipeproject", cipeproject);
		model.addAttribute("partnerList", partnerService.findAll());
		model.addAttribute("cipeprojecttypeList", cipeprojecttypeService.findAll());
		model.addAttribute("cipeprojectstatusList", cipeprojectstatusService.findAll());
		model.addAttribute("regionList", regionService.findAll());
		model.addAttribute("countryList", countryService.findAll());
		model.addAttribute("seniorProgramOfficerList", userService.findAll());
		model.addAttribute("programOfficerList", userService.findAll());
		model.addAttribute("programAssistantList", userService.findAll());
		/* Request from paymentRequest view */
		if (cipeproject.getId() != null) {
			model.addAttribute("cipeprojectList", cipeprojectService.findCipeproject(cipeproject.getId(), cipeproject.getPrc()));
		} else {/* From Dashboard */
			model.addAttribute("cipeprojectList", cipeprojectService.findCipeproject(cipeproject.getPrc()));
		}
		model.addAttribute("message", cipeproject.getMessage());
		return "findCipeproject";
	}

	@RequestMapping(value = "/secure/getCipeprojectBean.do", method = RequestMethod.GET)
	public @ResponseBody
	CipeprojectBean getCipeproject(@RequestParam("cipeprojectId") Integer id) {
		return cipeprojectService.getCipeproject(id);
	}

	@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request) {
	}

	@RequestMapping(value = "/secure/loadProjectStates.do", method = RequestMethod.GET)
	public @ResponseBody
	Map<String, String> loadStates(@RequestParam Integer countryId, Model model) {
		Map<String, String> results = null;
		Country country = countryService.findById(countryId);
		if (country != null) {
			List<State> states = country.getStates();
			results = new HashMap<String, String>();
			/*
			 * Note: we could let Spring serialize the list of states
			 * automatically, but for this list it reports an error.. the
			 * bidirectional mapping between country & state in the underlying
			 * beans confuse the jackson's serializer... so it is done manually
			 */
			if (states != null && !states.isEmpty()) {
				for (State state : states) {
					// TODO: this actually acts like a LIFO in the front end,
					// try to sort them first...
					results.put(state.getId().toString(), state.getDisplayName());
				}
			}
		}
		return results;
	}

	@RequestMapping("/secure/saveCipeproject.do")
	public String saveCipeproject(@ModelAttribute("cipeproject") Cipeproject cipeproject, BindingResult result, Model model, HttpServletRequest request) {
		int regId = cipeproject.getRegion().getId();
		try {

			System.out.println("region... " + cipeproject.getRegion().getId());
			cipeproject = cipeprojectService.saveCipeproject(cipeproject, getUserId(request), true);
			saveMessage(request, getText("editCipeproject.message.success"));
		} catch (CipeprojectException e) {
			logger.error(getText("editCipeproject.message.duplicate.projectId"), e);
			saveError(request, getText("editCipeproject.message.duplicate.projectId"));
		}
		model.addAttribute("cipeproject", cipeproject);
		model.addAttribute("partnerList", partnerService.findAll());
		model.addAttribute("donorawardList", donorawardService.getActiveDonoraward());
		model.addAttribute("cipeprojecttypeList", cipeprojecttypeService.findAll());
		model.addAttribute("cipeprojectstatusList", cipeprojectstatusService.findAll());
		model.addAttribute("regionList", regionService.findAll());
		model.addAttribute("countryList", countryService.findAll()); // model.addAttribute("subidentifierList",
		List<User> users = userService.findAll();
		model.addAttribute("seniorProgramOfficerList", users);
		model.addAttribute("programOfficerList", users);
		model.addAttribute("programAssistantList", users);
		model.addAttribute("parentCipeprojectList", cipeprojectService.findAll());
		model.addAttribute("stateList", stateService.findAll());
		Region region = regionService.findById(regId);
		List<Country> countries = region.getCountries();
		model.addAttribute("countryList", countries);
		return editCipeprojectView;
	}

	@RequestMapping("/secure/saveCipeprojectDetail.do")
	public String saveCipeprojectDetail(@ModelAttribute("cipeproject") Cipeproject cipeproject, BindingResult result, Model model, HttpServletRequest request) {
		try {
			cipeproject = cipeprojectService.saveCipeproject(cipeproject, getUserId(request), false);
			saveMessage(request, getText("editCipeproject.message.success"));
		} catch (CipeprojectException e) {
			logger.error(getText("editCipeproject.message.duplicate.projectId"), e);
			saveError(request, getText("editCipeproject.message.duplicate.projectId"));
		}
		model.addAttribute("cipeproject", cipeproject);
		model.addAttribute("partnerList", partnerService.findAll());
		model.addAttribute("donorawardList", donorawardService.getActiveDonoraward());
		model.addAttribute("cipeprojecttypeList", cipeprojecttypeService.findAll());
		model.addAttribute("cipeprojectstatusList", cipeprojectstatusService.findAll());
		model.addAttribute("regionList", regionService.findAll());
		model.addAttribute("countryList", countryService.findAll()); // model.addAttribute("subidentifierList",
		List<User> users = userService.findAll();
		model.addAttribute("seniorProgramOfficerList", users);
		model.addAttribute("programOfficerList", users);
		model.addAttribute("programAssistantList", users);
		model.addAttribute("parentCipeprojectList", cipeprojectService.findAll());
		model.addAttribute("stateList", stateService.findAll());
		Region region = regionService.findById(cipeproject.getRegion().getId());
		List<Country> countries = region.getCountries();
		model.addAttribute("countryList", countries);
		return EDIT_CIPEPROJECT_DETAIL;
	}

	@RequestMapping("/secure/savePartnergrant.do")
	public String savePartnergrant(@ModelAttribute("cipeproject") Cipeproject cipeproject, BindingResult result, Model model, HttpServletRequest request) {
		String targetPage = EDIT_PARTNER_GRANT_PAGE;
		if (request.getParameter("formName") != null) {
			targetPage = EDIT_PARTNER_GRANT_VIEW_PAGE;
		}
		if (cipeproject != null) {
			Cipeproject savedCipeproject = null;
			model.addAttribute("mode", request.getParameter("mode"));
			if (!result.hasErrors()) {
				if (cipeproject.getDonoraward() != null && cipeproject.getDonoraward().getId() == null) {
					cipeproject.setDonoraward(null);
				}
				try {
					savedCipeproject = cipeprojectService.saveOrUpdatePartnerGrantCipeProject(cipeproject, getUserId(request), true);
					if (StringUtils.isNotBlank(request.getParameter("addNarativeReport"))) {
						return ProjectReportController.REDIRECT_TO_REPORT_LIST_PAGE + savedCipeproject.getId();
					} else {
						saveMessage(request, getText("editCipeproject.message.successPartnerGrant"));
						savedCipeproject.setDonoraward(donorawardService.findById(savedCipeproject.getDonoraward().getId()));
						model.addAttribute("cipeproject", savedCipeproject);
						if (cipeproject.getId() == null || cipeproject.getId() == 0) {
							model.addAttribute("projectGrantSaved", true);
						}

						model.addAttribute("cipeprojectId", savedCipeproject.getId());
					}
				} catch (CipeprojectException e) {
					logger.error(getText("editCipeproject.message.duplicate.projectId"), e);
					model.addAttribute("cipeproject", cipeproject);
					saveError(request, getText("editCipeproject.message.duplicate.projectId"));
				}
			}
		} else {
			model.addAttribute("cipeproject", cipeproject);
			saveError(request, getText("editCipeproject.message.error"));
		}

		initAddEditPartnerGrantModelAttrs(model);

		return targetPage;
	}

	@RequestMapping("/secure/showAddCipeproject.do")
	public String showAddCipeproject(Model model) {
		model.addAttribute("cipeproject", new Cipeproject());
		initAddProjectModelAttrs(model);
		return editCipeprojectView;
	}

	@RequestMapping("/secure/showAddPartnergrant.do")
	public String showAddPartnergrant(Model model, HttpServletRequest request) {
		Cipeproject project = new Cipeproject();
		Date currentDate = new Date();
		project.setCreationDate(currentDate);
		project.setUpdationDate(currentDate);
		project.setFileLocation("S:\\Scanned Documents\\");
		Integer userId = (Integer) request.getSession().getAttribute(User.LOGGED_IN_USER_KEY);
		if (userId != null) {
			User dbUser = userService.findById(userId);
			project.setCreatedBy(dbUser);
			project.setUpdatedBy(dbUser);
		}
		model.addAttribute("cipeproject", project);
		model.addAttribute("mode", "addPartnerGrant");
		initAddEditPartnerGrantModelAttrs(model);
		return EDIT_PARTNER_GRANT_PAGE;
	}

	@RequestMapping("/secure/showCipeprojectList.do")
	public String showCipeprojectList(Model model) {
		model.addAttribute("cipeprojects", cipeprojectService.findAll());
		return "cipeprojectList";
	}

	@RequestMapping("/secure/showCipeprojectSummary.do")
	public String showCipeprojectSummary(@RequestParam Integer id, Model model) {
		if (null != id) {
			model.addAttribute("cipeproject", cipeprojectService.findById(id));
			model.addAttribute("childCipeprojectList", cipeprojectService.findById(id) != null ? cipeprojectService.findById(id).getChildProject() : "");
			return "editCipeprojectSummary";
		} else {
			return "";
		}
	}

	@RequestMapping("/secure/showCipeprojectModifications.do")
	public String showCipeprojectModifications(@RequestParam Integer id, Model model) {
		if (null != id) {
			model.addAttribute("cipeproject", cipeprojectService.findById(id));
			model.addAttribute("childCipeprojectList", cipeprojectService.findById(id) != null ? cipeprojectService.findById(id).getChildProject() : "");
			model.addAttribute("cipeprojectModificationsList", cipeprojectService.findById(id).getCipeprojectmodifications());
			return "viewCipeprojectModifications";
		} else {
			return "";
		}
	}

	@RequestMapping("/secure/showCipeprojectPaymentRequestLineItems.do")
	public String showCipeprojectPaymentRequestLineItems(@RequestParam Integer id, Model model) {
		if (null != id) {
			model.addAttribute("cipeproject", cipeprojectService.findById(id));
			model.addAttribute("cipeprojectPaymentRequestLineItemsList", cipeprojectService.findById(id).getPaymentrequestlineitems());
			return "viewCipeprojectPaymentRequestLineItems";
		} else {
			return "";
		}
	}

	@RequestMapping("/secure/showEditCipeproject.do")
	public String showEditCipeproject(@RequestParam Integer id, Model model) {
		model.addAttribute("cipeproject", cipeprojectService.findById(id));
		model.addAttribute("cipeprojectrollupList", cipeprojectService.findAll());
		model.addAttribute("partnerList", partnerService.findAll());
		model.addAttribute("donorawardList", donorawardService.getActiveDonoraward());
		model.addAttribute("cipeprojecttypeList", cipeprojecttypeService.findAll());
		model.addAttribute("cipeprojectstatusList", cipeprojectstatusService.findAll());
		model.addAttribute("regionList", regionService.findAll());
		model.addAttribute("countryList", countryService.findAll());
		model.addAttribute("seniorProgramOfficerList", userService.findAll());
		model.addAttribute("programOfficerList", userService.findAll());
		model.addAttribute("programAssistantList", userService.findAll());
		return editCipeprojectView;
	}

	@RequestMapping("/secure/showEditCipeprojectDetail.do")
	public String showEditCipeprojectDetail(@RequestParam Integer id, Model model) {
		Cipeproject cipeproject = cipeprojectService.findById(id);
		/*
		 * System.out.println(">>> region.id = " +
		 * cipeproject.getRegion().getId());
		 * System.out.println(">>> country.namme = " +
		 * cipeproject.getCountry().getDisplayName());
		 * System.out.println(">>> senior.program.officer = " +
		 * cipeproject.getSeniorProgramOfficer().getFullName());
		 * System.out.println(">>> program.officer = " +
		 * cipeproject.getProgramOfficer().getFullName());
		 * System.out.println(">>> program.assistant = " +
		 * cipeproject.getProgramAssistant().getFullName());
		 */
		model.addAttribute("cipeproject", cipeproject);
		model.addAttribute("cipeprojectrollupList", cipeprojectService.findAll());
		model.addAttribute("partnerList", partnerService.findAll());
		model.addAttribute("donorawardList", donorawardService.getActiveDonoraward());
		model.addAttribute("cipeprojecttypeList", cipeprojecttypeService.findAll());
		model.addAttribute("cipeprojectstatusList", cipeprojectstatusService.findAll());
		model.addAttribute("regionList", regionService.findAll());
		model.addAttribute("countryList", countryService.findAll());
		model.addAttribute("seniorProgramOfficerList", userService.findAll());
		model.addAttribute("programOfficerList", userService.findAll());
		model.addAttribute("programAssistantList", userService.findAll());
		model.addAttribute("childCipeprojectList", cipeprojectService.findById(id).getChildProject());
		model.addAttribute("parentCipeprojectList", cipeprojectService.findAll());
		return "editCipeprojectDetail";
	}

	@RequestMapping("/secure/showEditPartnergrant.do")
	public String showEditPartnergrant(@RequestParam Integer id, Model model) {
		model.addAttribute("cipeproject", cipeprojectService.findById(id));
		initAddEditPartnerGrantModelAttrs(model);
		return EDIT_PARTNER_GRANT_VIEW_PAGE;
	}

	@RequestMapping("/secure/showAddPartnergrantWithProject.do")
	public String showAddPartnergrantWithProject(Model model, HttpServletRequest request, @RequestParam("cipeprojectId") Integer id) {
		Cipeproject project = new Cipeproject();
		Date currentDate = new Date();
		project.setCreationDate(currentDate);
		project.setUpdationDate(currentDate);
		project.setFileLocation("S:\\Scanned Documents\\");
		Integer userId = (Integer) request.getSession().getAttribute(User.LOGGED_IN_USER_KEY);
		if (userId != null) {
			User dbUser = userService.findById(userId);
			project.setCreatedBy(dbUser);
			project.setUpdatedBy(dbUser);
		}
		project.setParentProject(this.cipeprojectService.findById(id));
		model.addAttribute("cipeproject", project);
		initAddEditPartnerGrantModelAttrs(model);
		return EDIT_PARTNER_GRANT_PAGE;
	}

	@RequestMapping("/secure/showAddPartnergrantWithPartner.do")
	public String showAddPartnergrantWithPartner(Model model, HttpServletRequest request, @RequestParam("partnerId") Integer id) {
		Cipeproject project = new Cipeproject();
		Date currentDate = new Date();
		project.setCreationDate(currentDate);
		project.setUpdationDate(currentDate);
		project.setFileLocation("S:\\Scanned Documents\\");
		Integer userId = (Integer) request.getSession().getAttribute(User.LOGGED_IN_USER_KEY);
		if (userId != null) {
			User dbUser = userService.findById(userId);
			project.setCreatedBy(dbUser);
			project.setUpdatedBy(dbUser);
		}
		project.setPartner(this.partnerService.findById(id));
		model.addAttribute("cipeproject", project);
		initAddEditPartnerGrantModelAttrs(model);
		return EDIT_PARTNER_GRANT_PAGE;
	}

	private void initAddProjectModelAttrs(Model model) {
		model.addAttribute("cipeprojectrollupList", cipeprojectService.findAll());
		model.addAttribute("partnerList", partnerService.findAll());
		model.addAttribute("donorawardList", donorawardService.getActiveDonoraward());
		model.addAttribute("cipeprojecttypeList", cipeprojecttypeService.getAllProjectTypesForProject());
		model.addAttribute("cipeprojectstatusList", cipeprojectstatusService.findAll());
		model.addAttribute("regionList", regionService.findAll());
		model.addAttribute("countryList", countryService.findAll());
		List<User> users = userService.findAll();
		model.addAttribute("seniorProgramOfficerList", users);
		model.addAttribute("programOfficerList", users);
		model.addAttribute("programAssistantList", users);
		model.addAttribute("parentCipeprojectList", cipeprojectService.findAll());
		model.addAttribute("stateList", stateService.findAll());
	}

	@RequestMapping("/secure/showAddCipeprojectWithPartner.do")
	public String showAddCipeprojectWithPartner(Model model, HttpServletRequest request, @RequestParam("partnerId") Integer id) {
		Cipeproject project = new Cipeproject();
		/*
		 * Cipeproject project = new Cipeproject(); Date currentDate = new
		 * Date(); project.setCreationDate(currentDate);
		 * project.setUpdationDate(currentDate); Integer userId = (Integer)
		 * request.getSession().getAttribute(User.LOGGED_IN_USER_KEY); if
		 * (userId != null) { User dbUser = userService.findById(userId);
		 * project.setCreatedBy(dbUser); project.setUpdatedBy(dbUser); }
		 */
		project.setPartner(this.partnerService.findById(id));
		model.addAttribute("cipeproject", project);
		initAddProjectModelAttrs(model);
		return editCipeprojectView;
	}

	private void initAddEditPartnerGrantModelAttrs(Model model) {
		model.addAttribute("partnerList", partnerService.findAll());
		model.addAttribute("donorawardList", donorawardService.getActiveDonoraward());
		model.addAttribute("cipeprojecttypeList", cipeprojecttypeService.findAll());
		model.addAttribute("cipeprojectstatusList", cipeprojectstatusService.findActiveAndPendingStatuses());
		model.addAttribute("regionList", regionService.findAllOrderBy("displayName"));
		model.addAttribute("countryList", countryService.findAll());
		model.addAttribute("seniorProgramOfficerList", userService.findAllByOrderByFullName());
		model.addAttribute("programOfficerList", userService.findAllByOrderByFullName());
		model.addAttribute("programAssistantList", userService.findAllByOrderByFullName());
		model.addAttribute("parentProjectList", cipeprojectService.getPartnerParentList());
	}

	@RequestMapping("/secure/showEditPartnergrantView.do")
	public String showEditPartnergrantView(@RequestParam Integer id, Model model) {
		Cipeproject cipeproject = cipeprojectService.findById(id);
		model.addAttribute("cipeproject", cipeproject);
		model.addAttribute("partnerList", partnerService.findAll());
		model.addAttribute("donorawardList", donorawardService.getActiveDonoraward());
		model.addAttribute("cipeprojecttypeList", cipeprojecttypeService.findAll());
		model.addAttribute("cipeprojectstatusList", cipeprojectstatusService.findAll());
		model.addAttribute("regionList", regionService.findAll());
		model.addAttribute("countryList", countryService.findAll());
		model.addAttribute("seniorProgramOfficerList", userService.findAllByOrderByFullName());
		model.addAttribute("programOfficerList", userService.findAllByOrderByFullName());
		model.addAttribute("programAssistantList", userService.findAllByOrderByFullName());
		// model.addAttribute("subidentifierList",
		// subidentifierService.findAll());
		model.addAttribute("childCipeprojectList", cipeprojectService.findById(id).getChildProject());
		float sumPaymentRequestAmount = 0;
		float burnRate = 0;
		for (Paymentrequestlineitem paymentrequestlineitem : cipeproject.getPaymentrequestlineitems()) {
			if (!(paymentrequestlineitem.getPaymentrequest().getStatusCode().equalsIgnoreCase(Constants.Cancelled) || paymentrequestlineitem.getPaymentrequest().getStatusCode()
					.equalsIgnoreCase(Constants.CancelRequested))) {
				sumPaymentRequestAmount += Util.getFloatValue(paymentrequestlineitem.getPaymentrequest().getAmount());
			}
		}
		burnRate = (sumPaymentRequestAmount / (new Date().getTime() - Util.getLongValue(cipeproject.getProjectStartDate())))
				/ ((Util.getFloatValue(cipeproject.getManagementFunds()) + Util.getFloatValue(cipeproject.getPartnerGrantFunds())) / ((Util.getLongValue(cipeproject
						.getProjectEndDate()) - Util.getLongValue(cipeproject.getProjectStartDate()))));
		model.addAttribute("currentTime", new Date().getTime());
		model.addAttribute("burnRate", burnRate);
		return "editPartnergrantView";
	}

	@RequestMapping("/secure/showPartnergrantList.do")
	public String showPartnergrantList(Model model) {
		model.addAttribute("cipeprojects", cipeprojectService.findAll());
		return "partnergrantList";
	}
}
